import { createRouter, createWebHistory } from 'vue-router'

import {Notify} from "vant";
import store from '../store'
const routes = [
  {
    path: '/',
    name: 'DefaultHome',
    component: ()=>import('../views/home/Home'),
    meta:{
      title:'首页'
    }
  },
  {
    path: '/home',
    name: 'Home',
    component: ()=>import('../views/home/Home'),
    meta:{
      title:'首页'
    }
  },
  {
    path: '/category',
    name: 'Category',
    component: ()=>import('../views/category/Categroy'),
    meta:{
      title:'分类'
    }
  },
  {
    path: '/detail',
    name: 'Detail',
    component: ()=>import('../views/detail/Detail'),
    meta:{
      title:'商品详情'
    }
  },
  {
    path: '/profile',
    name: 'Profile',
    component: ()=>import('../views/profile/Profile'),
    meta:{
      title:'个人中心',
      isAuthRequired:true
    }
  },
  {
    path: '/shopcart',
    name: 'Shopcart',
    component: ()=>import('../views/shopcart/ShopCart'),
    meta:{
      title:'购物车',
      isAuthRequired:true
    }
  },
  {
    path: '/register',
    name: 'Register',
    component: ()=>import('../views/profile/Register'),
    meta:{
      title:'用户注册'
    }
  },
  {
    path: '/login',
    name: 'Login',
    component: ()=>import('../views/profile/Login'),
    meta:{
      title:'用户登录'
    }
  },
  {
    path: '/address',
    name: 'Address',
    component: ()=>import('../views/profile/Address'),
    meta:{
      title:'地址列表',
      isAuthRequired: true
    }
  },
  {
    path: '/addressedit',
    name: 'AddressEdit',
    component: ()=>import('../views/profile/AddressEdit'),
    meta:{
      title:'地址列表',
      isAuthRequired: true
    }
  },
  {
    path: '/createorder',
    name: 'CreateOrder',
    component: ()=>import('../views/order/CreateOrder'),
    meta:{
      title:'创建订单',
      isAuthRequired: true
    }
  },
  {
    path: '/orderdetail',
    name: 'OrderDetail',
    component: ()=>import('../views/order/OrderDetil'),
    meta:{
      title:'订单详情',
      isAuthRequired: true
    }
  },
  {
    path: '/order',
    name: 'Order',
    component: ()=>import('../views/order/Order'),
    meta:{
      title:'订单列表',
      isAuthRequired: true
    }
  },

]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
router.beforeEach((to,from,next)=>{
  //如果没有登录，这里到login
  if(to.meta.isAuthRequired && store.state.user.isLogin===false){
    Notify('您还没有登录, 请先登录')
    return next('/login')
  }
  next();
  document.title=to.meta.title;
})

export default router
